<template>
  <el-button icon="el-icon-download" size="small" @click="handleDownload">导出</el-button>
</template>
<script>
import { getArticleCateApi } from '@/api/contnet/cate'
import { getTagListApi } from '@/api/contnet/tag'
import { getArticleListApi } from '@/api/contnet/article'
import { getAdvPosListApi } from '@/api/adv/pos'
import { getAdvImgListApi } from '@/api/adv/img'
export default {
  name: 'ExportUpload',
  props: {
    // 判断是哪个页面使用的组件
    type: {
      type: String,
      default: ''
    }
  },
  data() {
    return {
      list: []
    }
  },
  methods: {
    async handleDownload() {
      let tHeader = []
      let EnName = {}
      // 文章分类
      if (this.type === 'cate') {
        const res = await getArticleCateApi({ page: 1, limit: 10000000 })
        this.list = res.data.data
        tHeader = ['分类ID', '分类名称', '图标class', '权重']
        EnName = {
          '分类ID': 'id',
          '分类名称': 'catename',
          '图标class': 'icon',
          '权重': 'sort_num'
        }
      }
      // 文章标签
      if (this.type === 'tag') {
        const res = await getTagListApi({ page: 1, limit: 10000000 })
        this.list = res.data.data
        tHeader = ['ID', '标签名称']
        EnName = {
          'ID': 'id',
          '标签名称': 'tagname'
        }
      }
      // 文章管理
      if (this.type === 'article') {
        const res = await getArticleListApi({ page: 1, limit: 10 })
        this.list = res.data.data
        tHeader = ['ID', '标题', '分类名', '图片', '创建时间', '是否推荐', '是否置顶', '文章状态']
        EnName = {
          'ID': 'id',
          '标题': 'title',
          '分类名': 'cateid',
          '图片': 'pic',
          '创建时间': 'create_date',
          '是否推荐': 'ishot',
          '是否置顶': 'istop',
          '文章状态': 'status'
        }
      }

      // 广告位
      if (this.type === 'advPos') {
        const { data: res } = await getAdvPosListApi({ page: 1, limit: 1000000 })
        this.list = res.data
        tHeader = ['广告位名称', '广告位描述', '广告图数量', '广告位尺寸']
        EnName = {
          '广告位名称': 'advposname',
          '广告位描述': 'advposdesc',
          '广告图数量': 'advposnum',
          '广告位尺寸': 'advpossize'
        }
      }

      // 广告图
      if (this.type === 'advImg') {
        const { data: res } = await getAdvImgListApi({ page: 1, limit: 1000000 })
        this.list = res.data
        tHeader = ['广告图位', '广告图描述', '广告图', '广告图链接']
        EnName = {
          '广告图位': 'advposname',
          '广告图描述': 'advimgdesc',
          '广告图': 'advimgsrc',
          '广告图链接': 'advimglink'
        }
      }
      const result = this.handleUpload(this.list, tHeader, EnName)
      import('@/vendor/Export2Excel').then(excel => {
        excel.export_json_to_excel({
          header: tHeader,
          data: result,
          filename: '分类列表',
          autoWidth: true,
          bookType: 'xlsx'
        })
      })
    },
    handleUpload(list, tHeader, EnName) {
      const newArr = []
      // console.log(list, tHeader, EnName)
      list.forEach(item => {
        const arr = []
        tHeader.forEach(key => {
          // 遍历数据的key值
          const value = item[EnName[key]]
          // item[EnName[EnName[key]]] 中转英
          arr.push(value)
        })
        newArr.push(arr)
      })
      return newArr
    }
  }
}
</script>
<style lang="scss" scoped>

</style>
